package xyz.sicau.blog.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * "ord:769" 序列化后的
     * "\xac\xed\x00\x05t\x00\aord:970" 没有序列化
     * redisTemplate。opsForValue() // string
     * redisTemplate。opsForList() // List
     * redisTemplate。opsForSet()
     * redisTemplate。opsForHash()
     * redisTemplate。opsForZSet()
     */
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        // 设置key的string序列号方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value的序列号方式json，
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
